Python中list的含义是什么?一文搞懂Python中list的方法和用法 您所在的位置:网站首页 python 定义一个列表 Python中list的含义是什么?一文搞懂Python中list的方法和用法

Python中list的含义是什么?一文搞懂Python中list的方法和用法

#Python中list的含义是什么?一文搞懂Python中list的方法和用法| 来源: 网络整理| 查看: 265

Python中的list为python中的常用数据类型,其为python中内建类,继承自object。接下来全面介绍list的常见方法及自己实现类Python list功能的类

定义list

>>> li = ["a", "b", "mpilgrim", "z", "example"] >>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[0] 'a' >>> li[4] 'example'

负的list 索引

>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[-1] 'example' >>> li[-3] 'mpilgrim' >>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[1:3] ['b', 'mpilgrim'] >>> li[1:-1] ['b', 'mpilgrim', 'z'] >>> li[0:3] ['a', 'b', 'mpilgrim']

向 list 中增加元素

>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li.append("new") >>> li ['a', 'b', 'mpilgrim', 'z', 'example', 'new'] >>> li.insert(2, "new") >>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new'] >>> li.extend(["two", "elements"]) >>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']

遍历

list1 = [x for x in range(0,10,2)] # 方法一 for i in range(len(list1)): print(list1[i], end=' ') # 方法二 for x in list1: print(x, end=' ') # 方法三 for ind,value in enumerate(list1): print(ind, value, sep='=', end = ' ')

以上为个人理解,如有误解或错误,请指正

以上就是Python中list的含义是什么?一文搞懂Python中list的方法和用法的详细内容,更多请关注php中文网其它相关文章!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有